import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:material_collection_app/utils/dio_utils.dart';
import 'package:material_collection_app/utils/event_bus_util.dart';
import 'package:material_collection_app/widget/common/refresh.dart';
import 'package:material_collection_app/widget/common/round_checkbox.dart';
import 'package:material_collection_app/widget/common/top_bar.dart';
import 'package:material_collection_app/widget/enter/add_material_dialog.dart';
import 'package:material_collection_app/widget/enter/enter_check_item.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import '../utils/utils.dart';

class TaskCheckPage extends StatefulWidget {
  final Map arguments;

  TaskCheckPage({Key key, this.arguments}) : super(key: key);

  @override
  State<StatefulWidget> createState() => MyState();
}

class MyState extends State<TaskCheckPage> {
  RefreshController _refreshController =
      RefreshController(initialRefresh: false);
  List materialList = [];
  int currentPage = 1;
  int lastPage = 1;
  List selected = [];
  bool shelvesLoadSuccess = false;
  bool isSelectedAll = false;
  int updateStoresId;
  List storeList = [];
  int storeIndex;
  List libraryList = [];
  int libraryIndex;
  List shelvesList = [];
  int shelvesIndex;
  int storeId;
  int libraryId;
  int shelvesId;
  bool storeLoadSuccess = false;
  bool libraryLoadSuccess = false;
  bool shelvesSuccess = false;

  var eventBus = new EventBus();

  @override
  void initState() {
    super.initState();
    getList();
    getStoreList();
  }

  // 物资列表
  getList({bool loadMore = false}) {
    int id = widget.arguments['id'];

    if (loadMore) {
      currentPage++;
    } else {
      currentPage = 1;
    }

    DioUtils.request("enter/getMaterialList",
        context: context,
        method: "POST",
        params: {
          'id': id,
          'current_page': currentPage,
        }, onSuccess: (res) {
      if (res['code'] == 1) {
        setState(() {
          if (loadMore) {
            materialList.addAll(res["data"]["data"]);
          } else {
            materialList = res["data"]["data"];
          }
          currentPage = res['data']['current_page'];
          lastPage = res['data']['last_page'];
        });
      } else {
        Utils.showText(text: res['msg']);
      }
    }, onError: (error) {});
  }

  // 获取仓库列表
  getStoreList() {
    setState(() {
      storeId = null;
      libraryId = null;
      shelvesId = null;
      storeLoadSuccess = false;
    });
    DioUtils.request("store/LibraryAddInstall",
        context: context, method: "POST", params: {}, onSuccess: (res) {
      if (res['code'] == 1) {
        setState(() {
          storeLoadSuccess = true;
          storeList = res['data']['store'];
        });
      } else {
        Utils.showText(text: res['msg']);
      }
    }, onError: (error) {});
  }

  // 获取库区列表
  getLibraryList(id, libraryId, removeChildData) {
    setState(() {
      libraryLoadSuccess = false;
    });

    DioUtils.request("store/LibraryLists",
        context: context,
        method: "POST",
        params: {'store_id': id, 'limit': 100000}, onSuccess: (res) {
      if (res['code'] == 1) {
        setState(() {
          libraryLoadSuccess = true;
          libraryList = res['data']['data'];
        });

        if (libraryId != null) {
          // 设置库区默认索引
          for (int i = 0; i < libraryList.length; i++) {
            var item = libraryList[i];
            if (libraryId == item['store_library_id']) {
              setState(() {
                libraryIndex = i;
              });
              break;
            }
          }
        }
      } else {
        Utils.showText(text: res['msg']);
      }
    }, onError: (error) {});
  }

  // 获取库位列表
  getShelvesList(id, shelvesId, removeChildData) {
    setState(() {
      shelvesLoadSuccess = false;
    });

    DioUtils.request("storeDetail/lists",
        context: context,
        method: "POST",
        params: {'store_library_id': id, 'limit': 100000}, onSuccess: (res) {
      if (res['code'] == 1) {
        setState(() {
          shelvesLoadSuccess = true;
          shelvesList = res['data']['data'];
        });

        if (shelvesId != null) {
          for (int i = 0; i < shelvesList.length; i++) {
            var item = shelvesList[i];
            if (shelvesId == item['store_detail_id']) {
              setState(() {
                shelvesIndex = i;
              });
              break;
            }
          }
        }
      } else {
        Utils.showText(text: res['msg']);
      }
    }, onError: (error) {});
  }

  // 绑定RFID
  bindRFID() {
    int id = widget.arguments['id'];

    DioUtils.request("enter/bindrfid",
        context: context,
        method: "POST",
        params: {
          'id': id,
          // 'rfid':
          // 'material_id':
        }, onSuccess: (res) {
      if (res['code'] == 1) {
      } else {
        Utils.showText(text: res['msg']);
      }
    }, onError: (error) {});
  }

  // 确认信息
  _confirmInfo(int id, int index) {
    DioUtils.request("enter/confirmMaterial",
        context: context,
        method: "POST",
        params: {
          'id': id,
        }, onSuccess: (res) {
      if (res['code'] == 1) {
        setState(() {
          materialList[index]['confirm'] = 1;
        });
        Navigator.of(context).pop();
      } else {
        Utils.showText(text: res['msg']);
      }
    }, onError: (error) {});
  }

  // 获取库位
  storeDetail(int id, int itemId) {
    shelvesLoadSuccess = false;
    DioUtils.request("storeDetail/lists",
        context: context,
        method: "POST",
        params: {'store_library_id': id, 'limit': 100000}, onSuccess: (res) {
      if (res['code'] == 1) {
        setState(() {
          shelvesList = res['data']['data'];
          shelvesLoadSuccess = true;
        });
        showShelvesDialog(itemId);
      } else {
        Utils.showText(text: res['msg']);
      }
    }, onError: (error) {});
  }

  // 设置库位
  setShelves(id, shelvesId, shelvesName) {
    DioUtils.request("enter/updMaterial",
        context: context,
        method: "POST",
        params: {
          'id': id,
          'shelves_id': shelvesId,
        }, onSuccess: (res) {
      if (res['code'] == 1) {
        setState(() {
          materialList[0]['shelves'] = {
            'store_detail_id': shelvesId,
            'detail_code': shelvesName
          };
        });
        Navigator.of(context).pop();
      } else {
        Utils.showText(text: res['msg']);
      }
    }, onError: (error) {});
  }

  // 设置仓库、库区、库位
  setStores() {
    if (storeId == null) {
      Utils.showText(text: '请选择仓库');
      return;
    }

    if (libraryId == null) {
      Utils.showText(text: '请选择库区');
      return;
    }

    if (shelvesId == null) {
      Utils.showText(text: '请选择库位');
      return;
    }

    DioUtils.request("enter/updMaterial",
        context: context,
        method: "POST",
        params: {
          'id': updateStoresId,
          'store_id': storeId,
          'library_id': libraryId,
          'shelves_id': shelvesId,
        }, onSuccess: (res) {
      if (res['code'] == 1) {
        Navigator.pop(context);
        getList();
      } else {
        Utils.showText(text: res['msg']);
      }
    }, onError: (error) {});
  }

  // 库位选择
  showShelvesDialog(id) {
    List<Widget> widget = [];

    for (int i = 0; i < shelvesList.length; i++) {
      var item = shelvesList[i];

      widget.add(InkWell(
          onTap: () {
            setShelves(id, item['store_detail_id'], item['detail_code']);
          },
          child: Container(
              width: double.infinity,
              height: 60,
              alignment: Alignment.centerLeft,
              padding: EdgeInsets.symmetric(horizontal: 50.w),
              decoration: BoxDecoration(
                  // color: shelvesIndex == i ? Utils.hexToColor('#F2F2F2') : Colors.white,
                  border: Border(
                      bottom: BorderSide(
                          width: 1, color: Utils.hexToColor('#F7F7F7')))),
              child:
                  Text(item['detail_code'], style: TextStyle(fontSize: 15)))));
    }
  }

  // 下拉刷新
  void _onRefresh() async {
    _refreshController.loadComplete();
    getList();

    await Future.delayed(Duration(milliseconds: 600));
    _refreshController.refreshCompleted();
  }

  // 加载更多
  void _onLoading() async {
    await Future.delayed(Duration(milliseconds: 300));

    if (currentPage == lastPage) {
      _refreshController.loadNoData();
      return;
    }

    // 加载更多数据
    if (currentPage < lastPage) {
      getList(loadMore: true);
      _refreshController.loadComplete();
    }
  }

  // 处理默认数据
  handleDefaultAction(sId, lId, shId, updateId) {
    setState(() {
      storeIndex = null;
      libraryIndex = null;
      shelvesIndex = null;
      storeId = sId;
      libraryId = lId;
      shelvesId = shId;
      updateStoresId = updateId;
    });

    if (sId == null) {
      setState(() {
        libraryList = [];
        shelvesList = [];
        storeIndex = null;
        libraryIndex = null;
        shelvesIndex = null;
      });
    }
  }

  // 设置仓库默认索引
  setStoreIndex() {
    for (int i = 0; i < storeList.length; i++) {
      var item = storeList[i];
      if (storeId == item['store_id']) {
        setState(() {
          storeIndex = i;
        });
        break;
      }
    }
  }

  // 获取仓库、库区、库位信息
  getStoreInfo(int sId, int lId, int shId, BuildContext context) {
    if (sId != null) {
      setStoreIndex();
      getLibraryList(sId, lId, false);
    }

    if (lId != null) {
      getShelvesList(lId, shId, false);
    }

    Scaffold.of(context).openEndDrawer();
  }

  // 提交
  submit() {
    int id = widget.arguments['id'];

    DioUtils.request("enter/check", context: context, method: "POST", params: {
      'id': id,
      'current_page': currentPage,
    }, onSuccess: (res) {
      if (res['code'] == 1) {
        this.showSubmitDialog();
        eventBus
            .emit('task_enter_check', {'index': widget.arguments['itemIndex']});
      } else {
        Utils.showText(text: res['msg']);
      }
    }, onError: (error) {});
  }

  Widget _buildDiaabledInput(title, value) {
    return Container(
      margin: EdgeInsets.only(bottom: 20.h),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Container(
            width: 160.w,
            height: 40,
            alignment: Alignment.centerRight,
            child: Text(
              title + '：',
              style:
                  TextStyle(color: Utils.hexToColor('#999999'), fontSize: 14),
            ),
          ),
          Container(
            width: 460.w,
            height: 40,
            alignment: Alignment.centerLeft,
            padding: EdgeInsets.symmetric(horizontal: 15.w),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(4),
                border:
                    Border.all(color: Utils.hexToColor('#E6E6E6'), width: 1)),
            child: Container(
              child: Text(value.toString(),
                  style: TextStyle(
                      color: Utils.hexToColor('#333333'), fontSize: 14)),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildImageBox(item) {
    List imageList = item['goods_imgs'];
    List<Widget> imageWidget = [];

    for (var item in imageList) {
      var src = DioUtils.API_PREFIX + item;

      imageWidget.add(Container(
          child: Stack(children: <Widget>[
        ClipRRect(
          borderRadius: BorderRadius.all(Radius.circular(8.w)),
          child: Image.network(src,
              fit: BoxFit.cover, width: 136.w, height: 136.w),
        ),
      ])));
    }

    return Container(
        margin: EdgeInsets.only(bottom: 20.h),
        child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Container(
                width: 160.w,
                margin: EdgeInsets.only(top: 20.w),
                alignment: Alignment.centerRight,
                child: Text(
                  '图片：',
                  // textAlign: TextAlign.end,
                  style: TextStyle(
                      color: Utils.hexToColor('#999999'), fontSize: 14),
                ),
              ),
              Container(
                  width: 460.w,
                  child: Wrap(
                      spacing: 25.w, runSpacing: 25.w, children: imageWidget))
            ]));
  }

  Widget _buildCardTitle(title) {
    return Container(
      alignment: Alignment.centerLeft,
      padding: EdgeInsets.only(bottom: 10.w, top: 10.w),
      child: Text(
        title,
        style: TextStyle(
            color: Utils.hexToColor('#333333'),
            fontSize: 24.w,
            fontWeight: FontWeight.bold,
            height: 1),
      ),
    );
  }

  Widget _buildDialogHead(title) {
    return Container(
      height: 80.w,
      decoration: BoxDecoration(
          color: Utils.hexToColor('#F7F7F7'),
          borderRadius: BorderRadius.only(
              topLeft: Radius.circular(12.w), topRight: Radius.circular(12.w))),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Container(
            margin: EdgeInsets.only(left: 31.w),
            child: Text(title,
                style: TextStyle(
                    color: Utils.hexToColor('#333333'),
                    fontSize: 16,
                    fontWeight: FontWeight.bold)),
          ),
          InkWell(
            onTap: () {
              Navigator.of(context).pop();
            },
            child: Container(
              padding: EdgeInsets.fromLTRB(30.w, 24.w, 30.w, 24.w),
              alignment: Alignment.topRight,
              child: Image.asset(
                'assets/dialog_close.png',
                width: 36.w,
              ),
            ),
          )
        ],
      ),
    );
  }

  // 构建仓库列表
  Widget _buildStoreCard() {
    List<Widget> content = [];

    for (int i = 0; i < storeList.length; i++) {
      var item = storeList[i];

      content.add(InkWell(
        onTap: () {
          setState(() {
            libraryId = null;
            libraryList = [];
            libraryIndex = null;
            shelvesId = null;
            shelvesList = [];
            shelvesIndex = null;
            storeIndex = i;
            storeId = item['store_id'];
          });
          getLibraryList(item['store_id'], null, true);
        },
        child: Container(
          width: 290.w,
          height: 76.w,
          alignment: Alignment.center,
          color: Utils.hexToColor(storeIndex == i ? '#E7F3FF' : '#F8F8F8'),
          child: Text(
            item['store_name'],
            style: TextStyle(
                color:
                    Utils.hexToColor(storeIndex == i ? '#297CFF' : '#676767'),
                fontSize: 12,
                fontWeight:
                    storeIndex == i ? FontWeight.bold : FontWeight.w500),
          ),
        ),
      ));
    }

    return Container(
        padding: EdgeInsets.only(bottom: 15),
        margin: EdgeInsets.only(bottom: 15),
        decoration: BoxDecoration(
            border: Border(
                bottom:
                    BorderSide(width: 1, color: Utils.hexToColor('#F2F2F2')))),
        child: Wrap(
          spacing: 10.w,
          runSpacing: 10.w,
          crossAxisAlignment: WrapCrossAlignment.start,
          children: content,
        ));
  }

  // 构建库区列表
  Widget _buildLibraryCard() {
    List<Widget> content = [];

    if (libraryList.length > 0) {
      for (int i = 0; i < libraryList.length; i++) {
        var item = libraryList[i];

        content.add(InkWell(
          onTap: () {
            setState(() {
              shelvesId = null;
              shelvesList = [];
              shelvesIndex = null;
              libraryIndex = i;
              libraryId = item['store_library_id'];
            });
            getShelvesList(item['store_library_id'], null, true);
          },
          child: Container(
            width: 290.w,
            height: 76.w,
            alignment: Alignment.center,
            color: Utils.hexToColor(libraryIndex == i ? '#E7F3FF' : '#F8F8F8'),
            child: Text(
              item['library_name'],
              style: TextStyle(
                  color: Utils.hexToColor(
                      libraryIndex == i ? '#297CFF' : '#676767'),
                  fontSize: 12,
                  fontWeight:
                      libraryIndex == i ? FontWeight.bold : FontWeight.w500),
            ),
          ),
        ));
      }
    } else {
      if (storeIndex == null || libraryLoadSuccess == true) {
        content.add(Container(
          child: Text(
            storeIndex == null ? '请选择仓库' : '该仓库暂无库区',
            style: TextStyle(
              color: Utils.hexToColor('#676767'),
              fontSize: 12,
            ),
          ),
        ));
      } else {
        content.add(Container(
          child: Text(
            '加载中...',
            style: TextStyle(
              color: Utils.hexToColor('#676767'),
              fontSize: 12,
            ),
          ),
        ));
      }
    }

    return Container(
        padding: EdgeInsets.only(bottom: 15),
        margin: EdgeInsets.only(bottom: 15),
        decoration: BoxDecoration(
            border: Border(
                bottom:
                    BorderSide(width: 1, color: Utils.hexToColor('#F2F2F2')))),
        child: Wrap(
          spacing: 10.w,
          runSpacing: 10.w,
          crossAxisAlignment: WrapCrossAlignment.start,
          children: content,
        ));
  }

  // 构建库位列表
  Widget _buildshelvesCard() {
    List<Widget> content = [];

    if (shelvesList.length > 0) {
      for (int i = 0; i < shelvesList.length; i++) {
        var item = shelvesList[i];

        content.add(InkWell(
          onTap: () {
            setState(() {
              shelvesIndex = i;
              shelvesId = item['store_detail_id'];
            });
          },
          child: Container(
            width: 290.w,
            height: 76.w,
            alignment: Alignment.center,
            color: Utils.hexToColor(shelvesIndex == i ? '#E7F3FF' : '#F8F8F8'),
            child: Text(
              item['detail_code'],
              style: TextStyle(
                  color: Utils.hexToColor(
                      shelvesIndex == i ? '#297CFF' : '#676767'),
                  fontSize: 12,
                  fontWeight:
                      shelvesIndex == i ? FontWeight.bold : FontWeight.w500),
            ),
          ),
        ));
      }
    } else {
      if (libraryIndex == null || shelvesLoadSuccess == true) {
        content.add(Container(
          child: Text(
            libraryIndex == null ? '请选择库区' : '该库区暂无库位',
            style: TextStyle(
              color: Utils.hexToColor('#676767'),
              fontSize: 12,
            ),
          ),
        ));
      } else {
        content.add(Container(
          child: Text(
            '加载中...',
            style: TextStyle(
              color: Utils.hexToColor('#676767'),
              fontSize: 12,
            ),
          ),
        ));
      }
    }

    return Container(
        padding: EdgeInsets.only(bottom: 15),
        margin: EdgeInsets.only(bottom: 15),
        decoration: BoxDecoration(
            border: Border(
                bottom:
                    BorderSide(width: 1, color: Utils.hexToColor('#F2F2F2')))),
        child: Wrap(
          spacing: 10.w,
          runSpacing: 10.w,
          crossAxisAlignment: WrapCrossAlignment.start,
          children: content,
        ));
  }

  Widget _buildBottomButtons() {
    return Align(
      alignment: Alignment.bottomCenter,
      child: Container(
        width: 690.w,
        height: 140.w,
        margin: EdgeInsets.only(bottom: 30.w),
        child: Stack(
          children: <Widget>[
            _buildBottomButton(
                '添加物资',
                'add',
                'assets/add.png',
                Alignment.centerLeft,
                BorderRadius.horizontal(left: Radius.circular(30))),
            _buildBottomButton(
              '提交',
              'submit',
              'assets/submit.png',
              Alignment.centerRight,
              BorderRadius.horizontal(right: Radius.circular(30)),
            ),
            Center(
              child: Container(
                width: 3.w,
                height: 40.w,
                color: Utils.hexToColor('#7EB1FF'),
              ),
            )
          ],
        ),
      ),
    );
  }

  // 构建底部按钮
  Widget _buildBottomButton(String title, String type, String image,
      Alignment align, BorderRadius borderRadius) {
    return Align(
      alignment: align,
      child: Container(
        child: SizedBox(
          width: 345.w,
          height: 106.w,
          child: ElevatedButton(
            onPressed: () {
              if (type == 'add') {
                this.showAddMaterialDialog('add');
              }

              if (type == 'submit') {
                Utils.openAlert('确定要提交吗？', context).then((confirm) => {
                      if (confirm) {submit()}
                    });
              }
            },
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Container(
                    margin: EdgeInsets.only(left: 30.w),
                    child: Image.asset(image, width: 25.w)),
                Container(
                  margin: EdgeInsets.only(left: 16.w),
                  child: Text(
                    title,
                    style: TextStyle(color: Colors.white),
                  ),
                )
              ],
            ),
            style: ElevatedButton.styleFrom(
              backgroundColor: Color(0xFF297CFF),
              shape: RoundedRectangleBorder(borderRadius: borderRadius),
            ),
          ),
        ),
      ),
    );
  }

  // 添加物资 || 编辑物资
  Future<void> showAddMaterialDialog(String type, {Map item}) async {
    if (type == 'edit') {
      setState(() {
        updateStoresId = item['id'];
      });
    }

    showDialog(
        context: context,
        builder: (context) {
          return Dialog(
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(12.w))),
              insetPadding: EdgeInsets.all(0),
              child: AddUpdateMaterialDialog(
                type: type,
                item: item,
                orderId: widget.arguments['id'],
                onSubmit: (e) {
                  getList();
                },
              ));
        });
  }

  // 确认信息
  Future<void> showConfirmInfoDialog(int id, Map item, int index) async {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return Dialog(
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(12.w))),
              insetPadding: EdgeInsets.all(0),
              child: Container(
                  width: 700.w,
                  height: 1161.w,
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(100))),
                  child: Column(
                    children: <Widget>[
                      _buildDialogHead('物资信息'),
                      Expanded(
                        flex: 1,
                        child: SingleChildScrollView(
                          child: Container(
                              padding: EdgeInsets.all(40.w),
                              child: Column(children: <Widget>[
                                _buildDiaabledInput('物资编号', item['name']),
                                _buildDiaabledInput('物资名称', item['name']),
                                _buildDiaabledInput(
                                    '物资类型',
                                    item['mtype'] != null
                                        ? item['mtype']['type_name']
                                        : ''),
                                _buildDiaabledInput('物资数量', item['num']),
                                _buildDiaabledInput('规格/型号', item['type']),
                                _buildDiaabledInput('单位', item['unit']),
                                _buildDiaabledInput('备注', item['remark']),
                                _buildImageBox(item)
                              ])),
                        ),
                      ),
                      Container(
                        height: 120.w,
                        // color: Utils.hexToColor('#FFFFFF'),
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                            border: Border(
                                top: BorderSide(
                                    width: 1,
                                    color: Utils.hexToColor('#F2F2F2')))),
                        child: SizedBox(
                          width: 620.w,
                          height: 76.w,
                          child: ElevatedButton(
                            onPressed: () {
                              _confirmInfo(id, index);
                            },
                            child: Text(
                              '确认信息',
                              style: TextStyle(color: Colors.white),
                            ),
                            style: ElevatedButton.styleFrom(
                              backgroundColor: Color(0xFF297CFF),
                              shape: RoundedRectangleBorder(
                                  borderRadius:
                                      BorderRadius.all(Radius.circular(60.w))),
                            ),
                          ),
                        ),
                      )
                    ],
                  )));
        });
  }

  Future<void> showScanBindRfidDialog() async {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return Dialog(
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(12.w))),
              insetPadding: EdgeInsets.all(0),
              child: Container(
                  width: 660.w,
                  height: 960.w,
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(100))),
                  child: Column(
                    children: <Widget>[
                      _buildDialogHead('绑定RFID'),
                      Expanded(
                          flex: 1,
                          child: SingleChildScrollView(
                            child: Container(
                              padding: EdgeInsets.all(40.w),
                              child: Column(
                                children: <Widget>[
                                  Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: <Widget>[
                                      Container(
                                        alignment: Alignment.centerRight,
                                        child: Text(
                                          'RFID号:',
                                          // textAlign: TextAlign.end,
                                          style: TextStyle(
                                              color:
                                                  Utils.hexToColor('#999999'),
                                              fontSize: 14),
                                        ),
                                      ),
                                      Container(
                                        width: 460.w,
                                        height: 70.h,
                                        child: TextField(
                                          style: TextStyle(
                                              fontSize: 14,
                                              color: Utils.hexToColor(
                                                  '#297CFF')), //输入文本的样式
                                          decoration: InputDecoration(
                                            hintStyle: TextStyle(
                                                color: Color(0xFFC8C8D9),
                                                fontSize: 14),
                                            contentPadding: EdgeInsets.fromLTRB(
                                                21.w, 0, 21.w, 0),
                                            fillColor: Colors.white,
                                            filled: true,
                                            enabledBorder: OutlineInputBorder(
                                              borderRadius: BorderRadius.all(
                                                Radius.circular(8.w),
                                              ),
                                              borderSide: BorderSide(
                                                color:
                                                    Utils.hexToColor('#D4D4D4'),
                                                width: 1,
                                              ),
                                            ),
                                            focusedBorder: OutlineInputBorder(
                                              borderSide: BorderSide(
                                                color:
                                                    Utils.hexToColor('#D4D4D4'),
                                                width: 1,
                                              ),
                                              borderRadius: BorderRadius.all(
                                                Radius.circular(8.w),
                                              ),
                                            ),
                                          ),
                                        ),
                                      ),
                                    ],
                                  ),
                                  Container(
                                    margin: EdgeInsets.only(top: 17.w),
                                    child: Row(
                                      children: <Widget>[
                                        Text('*',
                                            style: TextStyle(
                                                color:
                                                    Utils.hexToColor('#F2332F'),
                                                fontSize: 12)),
                                        Text('温馨提示：请对准所需要绑定的物资进行扫描',
                                            style: TextStyle(
                                                color:
                                                    Utils.hexToColor('#999999'),
                                                fontSize: 12))
                                      ],
                                    ),
                                  ),
                                  Container(
                                    width: 580.w,
                                    height: 480.w,
                                    margin: EdgeInsets.only(top: 26.w),
                                    decoration: BoxDecoration(
                                        color: Utils.hexToColor('#F7FAFF'),
                                        borderRadius:
                                            BorderRadius.circular(12.w)),
                                    child: Column(
                                      children: <Widget>[
                                        Container(
                                          height: 90.w,
                                          padding: EdgeInsets.symmetric(
                                              horizontal: 31.w),
                                          decoration: BoxDecoration(
                                              border: Border(
                                                  bottom: BorderSide(
                                                      width: 1,
                                                      color: Utils.hexToColor(
                                                          '#EDEEF1')))),
                                          child: Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceBetween,
                                            children: <Widget>[
                                              Text('01-02-03',
                                                  style: TextStyle(
                                                      color: Utils.hexToColor(
                                                          "#333333"),
                                                      fontSize: 14)),
                                              Container(
                                                child: RoundCheckBox(
                                                    select: false),
                                              )
                                            ],
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                  Container(
                                    margin: EdgeInsets.only(top: 20.w),
                                    child: Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.center,
                                      children: <Widget>[
                                        Image.asset('assets/error.png',
                                            width: 26.w),
                                        Text('扫描出多个RFID，请选择对应的RFID码',
                                            style: TextStyle(
                                                color:
                                                    Utils.hexToColor('#F54745'),
                                                fontSize: 12))
                                      ],
                                    ),
                                  )
                                ],
                              ),
                            ),
                          )),
                      Container(
                        height: 120.w,
                        alignment: Alignment.center,
                        margin: EdgeInsets.only(bottom: 24.w),
                        child: SizedBox(
                          width: 380.w,
                          height: 76.w,
                          child: ElevatedButton(
                            onPressed: () {
                              Navigator.of(context).pop();
                            },
                            child: Text(
                              '保存',
                              style: TextStyle(color: Colors.white),
                            ),
                            style: ElevatedButton.styleFrom(
                              backgroundColor: Color(0xFF297CFF),
                              shape: RoundedRectangleBorder(
                                  borderRadius:
                                      BorderRadius.all(Radius.circular(60.w))),
                            ),
                          ),
                        ),
                      ),
                    ],
                  )));
        });
  }

  Future<void> showPrintRfidDialog() async {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return Dialog(
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(12.w))),
              insetPadding: EdgeInsets.all(0),
              child: Container(
                  width: 660.w,
                  height: 400.w,
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(100))),
                  child: Column(
                    children: <Widget>[
                      _buildDialogHead('打印RFID'),
                      Expanded(
                          flex: 1,
                          child: SingleChildScrollView(
                            child: Container(
                              padding: EdgeInsets.all(40.w),
                              child: Column(
                                children: <Widget>[
                                  Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: <Widget>[
                                      Container(
                                        alignment: Alignment.centerRight,
                                        child: Text(
                                          '打印数量:',
                                          // textAlign: TextAlign.end,
                                          style: TextStyle(
                                              color:
                                                  Utils.hexToColor('#999999'),
                                              fontSize: 14),
                                        ),
                                      ),
                                      Container(
                                        width: 440.w,
                                        height: 70.h,
                                        child: TextField(
                                          style: TextStyle(
                                              fontSize: 14,
                                              color: Colors.black), //输入文本的样式
                                          decoration: InputDecoration(
                                            hintText: "请填写RFID码打印数量",
                                            hintStyle: TextStyle(
                                                color: Color(0xFFC8C8D9),
                                                fontSize: 14),
                                            contentPadding: EdgeInsets.fromLTRB(
                                                21.w, 0, 21.w, 0),
                                            fillColor: Colors.white,
                                            filled: true,
                                            enabledBorder: OutlineInputBorder(
                                              borderRadius: BorderRadius.all(
                                                Radius.circular(8.w),
                                              ),
                                              borderSide: BorderSide(
                                                color:
                                                    Utils.hexToColor('#D4D4D4'),
                                                width: 1,
                                              ),
                                            ),
                                            focusedBorder: OutlineInputBorder(
                                              borderSide: BorderSide(
                                                color:
                                                    Utils.hexToColor('#D4D4D4'),
                                                width: 1,
                                              ),
                                              borderRadius: BorderRadius.all(
                                                Radius.circular(8.w),
                                              ),
                                            ),
                                          ),
                                        ),
                                      ),
                                    ],
                                  ),
                                ],
                              ),
                            ),
                          )),
                      Container(
                        height: 120.w,
                        margin: EdgeInsets.only(bottom: 24.w),
                        alignment: Alignment.center,
                        child: SizedBox(
                          width: 380.w,
                          height: 76.w,
                          child: ElevatedButton(
                            onPressed: () {
                              Navigator.of(context).pop();
                            },
                            child: Text(
                              '确定',
                              style: TextStyle(color: Colors.white),
                            ),
                            style: ElevatedButton.styleFrom(
                              backgroundColor: Color(0xFF297CFF),
                              shape: RoundedRectangleBorder(
                                  borderRadius:
                                      BorderRadius.all(Radius.circular(60.w))),
                            ),
                          ),
                        ),
                      ),
                    ],
                  )));
        });
  }

  Future<void> showSubmitDialog() async {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return Dialog(
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(12.w))),
              insetPadding: EdgeInsets.all(0),
              child: Container(
                  width: 660.w,
                  height: 400.w,
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(100))),
                  child: Column(
                    children: <Widget>[
                      Column(
                        children: <Widget>[
                          Container(
                            margin: EdgeInsets.only(top: 80.w),
                            child: Text('已提交给信息组',
                                style: TextStyle(
                                    color: Utils.hexToColor('#333333'),
                                    fontSize: 16,
                                    fontWeight: FontWeight.bold)),
                          ),
                          Container(
                              margin: EdgeInsets.only(top: 20.w),
                              child: Text('物资清点完成，查看进度请点击订单详情',
                                  style: TextStyle(
                                      color: Utils.hexToColor('#999999'),
                                      fontSize: 12))),
                        ],
                      ),
                      Container(
                        height: 120.w,
                        margin: EdgeInsets.only(top: 70.w),
                        alignment: Alignment.center,
                        child: SizedBox(
                          width: 380.w,
                          height: 76.w,
                          child: ElevatedButton(
                            onPressed: () {
                              Navigator.of(context).pop();
                              Navigator.of(context).pop();
                            },
                            child: Text(
                              '我知道了',
                              style: TextStyle(color: Colors.white),
                            ),
                            style: ElevatedButton.styleFrom(
                              backgroundColor: Color(0xFF297CFF),
                              shape: RoundedRectangleBorder(
                                  borderRadius:
                                      BorderRadius.all(Radius.circular(60.w))),
                            ),
                          ),
                        ),
                      ),
                    ],
                  )));
        });
  }

  Widget _buildPage() {
    return Container(
        color: Utils.hexToColor('#F5F6FA'),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            Expanded(
                flex: 1,
                child: Refresh(
                    refreshController: _refreshController,
                    onRefresh: _onRefresh,
                    onLoading: _onLoading,
                    child: ListView.builder(
                        itemCount: materialList.length,
                        padding: EdgeInsets.only(bottom: 130.w),
                        itemBuilder: (BuildContext context, int index) {
                          var item = materialList[index];
                          selected.add({'selected': false});

                          return EnterCheckItem(
                              item: item,
                              itemIndex: index,
                              selected: selected,
                              getStoreInfo: getStoreInfo,
                              handleDefaultAction: handleDefaultAction,
                              showConfirmInfoDialog: showConfirmInfoDialog,
                              showAddMaterialDialog: showAddMaterialDialog);
                        })))
          ],
        ));
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false);

    return Scaffold(
        appBar: TopBar(
          title: "清点物资",
          actions: <Widget>[
            InkWell(
              onTap: () {
                for (int i = 0; i < selected.length; i++) {
                  setState(() {
                    selected[i]['selected'] = !isSelectedAll;
                  });
                }
                setState(() {
                  isSelectedAll = !isSelectedAll;
                });
              },
              child: Container(
                alignment: Alignment.center,
                height: double.infinity,
                padding: EdgeInsets.symmetric(horizontal: 31.w),
                child: Text('全选'),
              ),
            )
          ],
        ),
        body: SafeArea(
          child: Container(
            child: Column(children: <Widget>[
              // Container(
              //   height: 108.w,
              //   padding: EdgeInsets.all(24.w),
              //   color: Utils.hexToColor('#297CFF'),
              //   child: Row(
              //     children: <Widget>[
              //       _buildButton('打印RFID', '#409EFF', 'print'),
              //     ],
              //   ),
              // ),
              Expanded(
                flex: 1,
                child: Stack(
                  children: <Widget>[_buildPage(), _buildBottomButtons()],
                ),
              ),
            ]),
          ),
        ),
        endDrawer: Drawer(
            child: Container(
                color: Colors.white,
                padding: EdgeInsets.fromLTRB(20.w, 78.w, 20.w, 34.w),
                child: Stack(children: <Widget>[
                  SingleChildScrollView(
                    child: Column(children: <Widget>[
                      _buildCardTitle('仓库'),
                      _buildStoreCard(),
                      _buildCardTitle('库区'),
                      _buildLibraryCard(),
                      Offstage(
                        offstage: storeIndex != null && libraryIndex != null
                            ? false
                            : true,
                        child: Column(
                          children: <Widget>[
                            _buildCardTitle('库位'),
                            _buildshelvesCard(),
                          ],
                        ),
                      ),
                      Container(
                        height: 50,
                      )
                      // _buildCard(),
                    ]),
                  ),
                  Align(
                      alignment: Alignment.bottomCenter,
                      child: Container(
                          child: Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: <Widget>[
                            Container(
                                // margin: EdgeInsets.only(top: 70.w),
                                child: SizedBox(
                                    width: 550.w,
                                    height: 76.w,
                                    child: ElevatedButton(
                                      onPressed: () {
                                        setStores();
                                      },
                                      child: Container(
                                        margin: EdgeInsets.only(left: 16.w),
                                        child: Text(
                                          '确定',
                                          style: TextStyle(color: Colors.white),
                                        ),
                                      ),
                                      style: ElevatedButton.styleFrom(
                                          backgroundColor: Color(0xFF297CFF),
                                          shape: RoundedRectangleBorder(
                                              borderRadius:
                                                  BorderRadius.circular(60.w))),
                                    )))
                          ])))
                ]))));
  }
}
